﻿/**
 * MotionBlurFilter
 *
 * @author		Allen Chou
 * @version		1.0.2 (last update: Nov 24 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */
 
package idv.cjcat.filters {
	import flash.display.Shader;
	import flash.filters.ShaderFilter;
	import flash.utils.ByteArray;
	
	import idv.cjcat.utils.ByteArrayCodec;

	/**
	 * A MotionBlur filter.
	 */
	public class MotionBlurFilter extends ShaderFilter {
		
		public function MotionBlurFilter(amount:Number = 2, angle:Number = 0) {
			if (pbjByteArray == null) pbjByteArray = ByteArrayCodec.decode(pbjString);
			
			this.shader = new Shader(pbjByteArray);
			this.amount = amount;
			this.angle = angle;
		}
		
		/**
		 * The amount of motion blur.
		 */
		public function get amount():Number { return this.shader.data.amount.value[0]; }
		public function set amount(value:Number):void {
			this.shader.data.amount.value[0] = value;
			
			adjustExtensions();
		}
		
		/**
		 * The angle of motion blur (in radians), which represents the direction of motion.
		 */
		public function get angle():Number { return this.shader.data.angle.value[0]; }
		public function set angle(value:Number):void {
			while (value < 0) value += Math.PI;
			while (value > Math.PI) value -= Math.PI;
			this.shader.data.angle.value[0] = value;
			
			adjustExtensions();
		}
		
		private function adjustExtensions():void {
			this.topExtension = this.bottomExtension = amount * 5 * Math.abs(Math.sin(angle));
			this.leftExtension = this.rightExtension = amount * 5 * Math.abs(Math.cos(angle));
		}
		
		private static const pbjString:String = "2581808080248A80CDEFF4E9EFEEC2ECF5F2208CEEE1EDE5F3F0E1E3E580E9E4F6AEE3EAE3E1F4AEF0E9F8E5ECE2E5EEE4E5F280208CF6E5EEE4EFF280C1ECECE5EEA0C3E8EFF5802088F6E5F2F3E9EFEE808180208CE4E5F3E3F2E9F0F4E9EFEE80B5ADECE5F6E5ECA0EDEFF4E9EFEEA0E2ECF5F2AE8021818280808CDFCFF5F4C3EFEFF2E480218181808082E1EDEFF5EEF4802281EDE9EED6E1ECF5E580808080802281EDE1F8D6E1ECF5E580C2C880802281E4E5E6E1F5ECF4D6E1ECF5E580C0808080218181808081E1EEE7ECE5802281EDE9EED6E1ECF5E580808080802281EDE1F8D6E1ECF5E580C0498F502281E4E5E6E1F5ECF4D6E1ECF5E58080808080238084F3F2E38021828481808FE4F3F4808C838000808040809D828000838080808D838000808040809D8280C0838080808D83800080804080B28380C080808080828380C0838080809D8280A08380C0808C838000808040809D828090838080809D838084828080809D82804180809080B28280A080808080B2828090BF0080809D84804182803080A1848044838080809D8280B184809080B281800080808080B28180C080808080B28180A080808080B2818090808080809D848000808000809D8480E182803080838480E1848080809D85804182809080818580418480E080B0868071858090808181807386809B809D8480E182803080838480E1848080809D85804182809080828580418480E080B0868071858090808181807386809B8081848000808000809D8480E182803080838480E1848080809D85804182809080818580418480E080B0868071858090808181807386809B809D8480E182803080838480E1848080809D85804182809080828580418480E080B0868071858090808181807386809B8081848000808000809D8480E182803080838480E1848080809D85804182809080818580418480E080B0868071858090808181807386809B809D8480E182803080838480E1848080809D85804182809080828580418480E080B0868071858090808181807386809B8081848000808000809D8480E182803080838480E1848080809D85804182809080818580418480E080B0868071858090808181807386809B809D8480E182803080838480E1848080809D85804182809080828580418480E080B0868071858090808181807386809B8081848000808000809D8480E182803080838480E1848080809D85804182809080818580418480E080B0868071858090808181807386809B809D8480E182803080838480E1848080809D85804182809080828580418480E080B0868071858090808181807386809B80B28480C0C1A08080848580738480D5808381807385809B80";
		private static var pbjByteArray:ByteArray = null;
	}
}